from django.db import models
from utils.modelsMixin import DataTimeMixin, LogicDeleteMixin, BaseSetModel
from django.contrib.auth.models import User


# Create your models here.
# 标签模型
class Label(DataTimeMixin):
    name = models.CharField(max_length=100, verbose_name='标签名称')

    # 指定表名
    class Meta:
        db_table = 'label'

    # 重写方法，展示字段
    def __str__(self):
        return self.name


# 文章模型
class Article(BaseSetModel):
    STATUS_CHOICES = ((0, '未发布'), (1, '已发布'))
    title = models.CharField(max_length=100, verbose_name='文章标题')
    digest = models.CharField(max_length=300, verbose_name='文章摘要')
    content = models.TextField(verbose_name='文章内容')
    page_view = models.IntegerField(default=0, verbose_name='文章浏览量')
    status = models.IntegerField(choices=STATUS_CHOICES, default=0, verbose_name='文章状态')
    priority = models.IntegerField(default=0, verbose_name='文章优先级')
    label = models.ForeignKey(Label, on_delete=models.SET_NULL, null=True, verbose_name='文章标签')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')

    # 指定表名
    class Meta:
        db_table = 'article'
        # 设置优先展示规则
        ordering = ['-priority', '-page_view', '-create_time']

    # 重写方法,展示字段
    def __str__(self):
        return self.title


# 评论模型
class Comment(BaseSetModel):
    content = models.TextField(verbose_name='评论内容')
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name='文章')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
    # 设置父级评论
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,
                                       verbose_name='父级评论id',
                                       related_name='parent_comment_set')
    # 设置互相管理评论字段
    reply_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,
                                      verbose_name='回复评论id',
                                      related_name='reply_comment_set')

    # 设置表名
    class Meta:
        db_table = 'comment'
        # 设置优先展示规则
        ordering = ['-create_time']

    # 书写魔法方法
    def __str__(self):
        return f'{self.user}:{self.content}'
